package com.nwcl.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CheckUtil {

    private static List<String> IMG_SUFFIX = Arrays.asList(".JPEG", ".JPG", ".PNG", ".GIF");
    private static List<String> VIDEO_SUFFIX = Arrays.asList(".MP4", ".AVI", ".MOV", ".FLV", ".WMV", ".MKV", ".3GP", ".MPG");
    private static List<String> IMG_AND_VIDEO_SUFFIX = Arrays.asList(".JPEG", ".JPG", ".PNG", ".GIF", ".MP4", ".AVI", ".MOV", ".FLV", ".WMV", ".MKV", ".3GP", ".MPG");

    /**
     * 判断字符串中是否含有表情
     *
     * @param source
     * @return
     */
    public static boolean containsEmoji(String source) {

        int len = source.length();
        boolean isEmoji = false;
        for (int i = 0; i < len; i++) {
            char hs = source.charAt(i);
            if (0xd800 <= hs && hs <= 0xdbff) {
                if (source.length() > 1) {
                    char ls = source.charAt(i + 1);
                    int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                    if (0x1d000 <= uc && uc <= 0x1f77f) {
                        return true;
                    }
                }
            } else {
                // non surrogate
                if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
                    return true;
                } else if (0x2B05 <= hs && hs <= 0x2b07) {
                    return true;
                } else if (0x2934 <= hs && hs <= 0x2935) {
                    return true;
                } else if (0x3297 <= hs && hs <= 0x3299) {
                    return true;
                } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d
                        || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c
                        || hs == 0x2b1b || hs == 0x2b50 || hs == 0x231a) {
                    return true;
                }
                if (!isEmoji && source.length() > 1 && i < source.length() - 1) {
                    char ls = source.charAt(i + 1);
                    if (ls == 0x20e3) {
                        return true;
                    }
                }
            }
        }
        if (source.equals("\uD83E\uDD71")) {
            return true;
        }
        return isEmoji;
    }

    public static String filterEmoji(String str) {
        if (str == null) {
            return str;
        }
        Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\ud83e\udc00-\ud83e\udfff]|[\u2600-\u27ff]",
                Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher emojiMatcher = emoji.matcher(str);
        if (emojiMatcher.find()) {
            //将所获取的表情转换为*
            str = emojiMatcher.replaceAll("*");
            return str;
        }
        return str;
    }


    /**
     * 通过读取文件并获取其width及height的方式，来判断判断当前文件是否图片，这是一种非常简单的方式。
     *
     * @param imageFile
     * @return
     */
    public static boolean isImage(File imageFile) {
        if (!imageFile.exists()) {
            return false;
        }
        Image img = null;
        try {
            img = ImageIO.read(imageFile);
            if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            img = null;
        }
    }


    public static boolean checkImgFileSuffix(MultipartFile file) {
        return checkFileSuffix(file, IMG_SUFFIX);
    }

    public static boolean checkVideoFileSuffix(MultipartFile file) {
        return checkFileSuffix(file, VIDEO_SUFFIX);
    }

    public static boolean checkImgAndVideoFileSuffix(MultipartFile file) {
        return checkFileSuffix(file, IMG_AND_VIDEO_SUFFIX);
    }

    public static boolean checkFileSuffix(MultipartFile file, List<String> suffixList) {
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isBlank(originalFilename)) {
            return true;
        }
        int index = originalFilename.lastIndexOf(".");
        if (index < 0) {
            return true;
        }
        String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (!CollectionUtils.isEmpty(suffixList)) {
            String lower_fileSuffix = fileSuffix.toLowerCase();
            String upper_fileSuffix = fileSuffix.toUpperCase();
            if (suffixList.contains(lower_fileSuffix) || suffixList.contains(upper_fileSuffix)) {
                return true;
            }
        }
        return false;
    }

}
